/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 * Performance Profiler Implementation
 */

#include "PerformanceProfiler.hpp"
#include "log.hpp"
#include <algorithm>
#include <iomanip>
#include <sstream>

void PerformanceProfiler::PrintSummary() const {
    std::lock_guard<std::mutex> lock(m_mutex);
    
    GOLOGI("=== Vulkan API Performance Summary ===");
    GOLOGI("Total Frames: %llu", static_cast<unsigned long long>(m_currentFrame));
    GOLOGI("Total API Calls: %zu", m_records.size());
    GOLOGI("");
    
    // 创建排序后的统计列表(按平均时间降序)
    std::vector<ApiStats> sortedStats;
    sortedStats.reserve(m_stats.size());
    for (const auto& pair : m_stats) {
        sortedStats.push_back(pair.second);
    }
    
    std::sort(sortedStats.begin(), sortedStats.end(), 
              [](const ApiStats& a, const ApiStats& b) {
                  return a.avgTimeMicroseconds > b.avgTimeMicroseconds;
              });
    
    GOLOGI("%-40s %10s %15s %12s %12s %12s", 
           "API Name", "Calls", "Total(μs)", "Min(μs)", "Max(μs)", "Avg(μs)");
    GOLOGI("---------------------------------------- ---------- --------------- ------------ ------------ ------------");
    
    for (const auto& stats : sortedStats) {
        GOLOGI("%-40s %10llu %15.2f %12.2f %12.2f %12.2f",
               stats.apiName.c_str(),
               static_cast<unsigned long long>(stats.callCount),
               stats.totalTimeMicroseconds,
               stats.minTimeMicroseconds,
               stats.maxTimeMicroseconds,
               stats.avgTimeMicroseconds);
    }
    
    GOLOGI("==========================================");
}
